package com.swn.common.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.swn.common.constants.ICommonConstants;

/**
 * TODO WEB 环境解耦工具类
 * 
 * @author Leon
 * @Date 2016年3月29日  下午4:18:23
 */
public final class WebContextUtils {

	private static ThreadLocal<WebContextUtils> WEB_CONTEXT_HOLDER = new ThreadLocal<WebContextUtils>();
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	/**
	 * 私有构造器
	 */
	private WebContextUtils(HttpServletRequest request, HttpServletResponse response){
		this.request = request;
		this.response = response;
	}

	/**
	 * 初始化
	 */
	public static void init(HttpServletRequest request, HttpServletResponse response) {
		WebContextUtils.WEB_CONTEXT_HOLDER.set(new WebContextUtils(request, response));
	}

	/**
	 * 销毁
	 */
	public static void destroy(){
		WebContextUtils.WEB_CONTEXT_HOLDER.remove();
	}
	
	/**
	 * TODO 获取 HttpServletRequest
	 * @return HttpServletRequest HTTP request
	 */
	public static HttpServletRequest getRequest() {
		return WebContextUtils.WEB_CONTEXT_HOLDER.get().request;
	}

	/**
	 * TODO 获取	HttpServletResponse
	 * @return HttpServletResponse HTTP response
	 */
	public static HttpServletResponse getResponse() {
		return WebContextUtils.WEB_CONTEXT_HOLDER.get().response;
	}

	/**
	 * TODO 获取 HttpSession
	 * @return HttpSession HTTP Session
	 */
	public static HttpSession getSession(){
		return WebContextUtils.getRequest().getSession();
	}

	/**
	 * TODO 获取 ServletContext
	 * @return ServletContext ServletContext
	 */
	public static ServletContext getServletContext(){
		return WebContextUtils.getRequest().getServletContext();
	}

	/**
	 * TODO 设置	HttpServletRequest	属性	key-value
	 * 
	 * @param attrName String 属性	key
	 * @param attrValue Object 属性	value
	 */
	public static void setRequestAttribute(String attrName,Object attrValue){
		WebContextUtils.getRequest().setAttribute(attrName, attrValue);
	}

	/**
	 * TODO 获取	HttpServletRequest	属性	value
	 * 
	 * @param attrName String 属性	key
	 * @return T (T)Object 属性	value
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getRequestAttribute(String attrName){
		return (T) WebContextUtils.getRequest().getAttribute(attrName);
	}

	/**
	 * TODO 移除	HttpServletRequest	属性	key-value
	 * 
	 * @param attrName String 属性名
	 */
	public static void removeRequestAttribute(String attrName){
		WebContextUtils.getRequest().removeAttribute(attrName);
	}

	/**
	 * TODO 获取	HttpSession	属性	value
	 * 
	 * @param attrName String 属性	key
	 * @return T (T)Object 属性	value
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getSessionAttribute(String attrName){
		return (T) WebContextUtils.getSession().getAttribute(attrName);
	}
	/**
	 * TODO 设置	HttpSession	属性	key-value
	 * 
	 * @param attrName String 属性	key
	 * @param attrValue Object 属性	value
	 */
	public static void setSessionAttribute(String attrName,Object attrValue){
		WebContextUtils.getSession().setAttribute(attrName, attrValue);
	}
	
	/**
	 * TODO 设置	HttpSession	属性	key-value
	 * 
	 * @param attrName String 属性	key
	 * @param attrValue Object 属性	value
	 */
	public static void setApplyType(String attrName,Object attrValue){
		WebContextUtils.getSession().setAttribute(attrName, attrValue);
	}

	/**功能说明:<此处是项目功能>
	 *	
	 * @author  : Leon
	 * @date    : 2016-5-4 下午5:26:21
	 * @modifier: 
	 * @mod_Date: 
	 * @return
	 */
    public static Map<String, Object> getRequestParameterMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        Enumeration<String> names = getRequest().getParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            map.put(name, getRequest().getParameter(name));
        }
        return map;
    }


	/**
	 * TODO 移除	HttpSession	属性	key-value
	 * 
	 * @param attrName String 属性名
	 */
	public static void removeSessionAttribute(String attrName){
		WebContextUtils.getSession().removeAttribute(attrName);
	}
	
	/**
	 * 使 Session失效
	 */
	public static void invalidateSession(){
		WebContextUtils.getSession().invalidate();
	}
}
